import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义规则信息的类型
export interface RulesInfo {
    ruleId: (string | number)[]
    userid: (string | number)[]
    cameraCode: string[]
}

// 定义 rulesinfo store
export const useRulesInfoStore = defineStore('rulesinfo', () => {
    // 存储各个字段的数组
    const ruleIds = ref<(string | number)[]>([])
    const userIds = ref<(string | number)[]>([])
    const cameraCodes = ref<string[]>([])

    // 单独存储 ruleId（支持单个或数组）
    function addRuleId(ruleId: string | number | (string | number)[]) {
        if (Array.isArray(ruleId)) {
            ruleIds.value.push(...ruleId)
        } else {
            ruleIds.value.push(ruleId)
        }
    }

    // 单独存储 userid（支持单个或数组）
    function addUserId(userid: string | number | (string | number)[]) {
        if (Array.isArray(userid)) {
            userIds.value.push(...userid)
        } else {
            userIds.value.push(userid)
        }
    }

    // 单独存储 cameraCode（支持单个或数组）
    function addCameraCode(cameraCode: string | string[]) {
        if (Array.isArray(cameraCode)) {
            cameraCodes.value.push(...cameraCode)
        } else {
            cameraCodes.value.push(cameraCode)
        }
    }

    // 获取所有数据
    function getAllData() {
        return {
            ruleIds: ruleIds.value,
            userIds: userIds.value,
            cameraCodes: cameraCodes.value
        }
    }

    // 清空所有数据
    function clearAllData() {
        ruleIds.value = []
        userIds.value = []
        cameraCodes.value = []
    }

    return {
        // 状态
        ruleIds,
        userIds,
        cameraCodes,

        // 单独存储方法
        addRuleId,
        addUserId,
        addCameraCode,

        // 获取所有数据
        getAllData,

        // 清空所有数据
        clearAllData
    }
})